<!-- 转审弹窗 -->
<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="转审" width="600px" @open="openDialog" @close="closeDialog">
    <px-form ref="formRef" :model="formValue" :rules="rules" label-position="right" label-width="120px">
      <px-row>
        <px-col :span="24">
          <px-form-item label="转审人员" prop="transferId">
            <px-select-v2
              v-model="formValue.transferId"
              style="width: 100%"
              filterable
              clearable
              :options="transferIdList"
              placeholder="请选择转审人员"
              @change="transferIdChange"
            />
          </px-form-item>
        </px-col>
        <px-col :span="24">
          <px-form-item label="转审原因" prop="approvalOpinion">
            <px-input
              v-model="formValue.approvalOpinion"
              style="width: 100%"
              maxlength="500"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              placeholder="请输入转审原因"
            />
          </px-form-item>
        </px-col>
      </px-row>
    </px-form>
    <div style="margin-top: 56px; text-align: right">
      <px-button @click="closeDialog">取消</px-button>
      <px-button type="primary" @click="handleSubmit(formRef)">确认</px-button>
    </div>
  </px-dialog>
</template>

<script setup lang="ts">
import { toCheckOutbound, toCheckWarehousing } from "@/api/erc/spareParts";
import { getEmployeeIdAndName } from "@/utils/pengxi";
import { PxMessage } from "px-ui";
import { computed, reactive, ref, toRefs } from "vue";

// 调用父级-关闭弹窗、查询列表方法
const emits = defineEmits(["closeDialog"]);
// 父级传过来的值
const props = defineProps(["dialogVisible", "busId", "approverId", "approverName", "type"]);

const visible = computed(() => props.dialogVisible);
// 页面本身的值
const formRef = ref();
const state = reactive<any>({
  transferIdList: [],
  formValue: {
    transferId: null,
    approvalOpinion: ""
  }
});
const { transferIdList, formValue } = toRefs(state);

// 表单校验规则
const rules = computed(() => ({
  transferId: [{ required: true, message: "转审人员不能为空", trigger: "change" }],
  approvalOpinion: [{ required: true, message: "转审原因不能为空", trigger: "blur" }]
}));

// 获取转审人名称
const transferIdChange = val => {
  const arr = transferIdList.value.filter(item => {
    return item.value === val;
  });
  if (arr && arr.length > 0) {
    formValue.value.transferName = arr[0].label;
  }
};

// 打开弹窗
const openDialog = async () => {
  formRef.value.resetFields();
  const arr = await getEmployeeIdAndName(["HSE", "ISEP", "ERC&FP"]);
  const userInfo = JSON.parse(localStorage.getItem("userInfo"));
  transferIdList.value = arr.filter(item => {
    return item.userId !== userInfo.userId;
  });
};
// 关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};
// 提交
const handleSubmit = async (formEl: any) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      const params = formValue.value;
      params.busId = props.busId;
      params.operateId = props.approverId;
      params.operateName = props.approverName;
      if (props.type === "wareHousing") {
        await toCheckWarehousing(params);
      } else if (props.type === "outBound") {
        await toCheckOutbound(params);
      }
      PxMessage.success("转审成功");
      emits("closeDialog");
    }
  });
};
</script>

<style lang="scss" scoped>
@import url("@/views/erc/scss/add-dialog.scss");
</style>
